//index.js
//获取应用实例
const app = getApp()

import {
  banner,
  getCate,
  getCateII,
  lookAttack,
  login
} from '../../utils/api.js'

Page({
  data: {
    sision: true,
    dianjiValue: "",
    hiddenFlag: false,
    getToken: "",
    headImg: "",
    loginData: {
      code: "",
      nickname: "",
      head_img: "",
      sex: ""
    },
    currentIndex: 0,
    aa: 0,
    hideModal: true, //模态框的状态  true-隐藏  false-显示
    animationData: {},
    maskFlag: false,
    fontSize: 30,
    bFontSize: 40,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    imgUrls: [],
    indicatorDots: false,
    childrenList: [],
    getCateList: [],
    arrayList: [],
    autoplay: true,
    interval: 3000,
    duration: 1000,
    current: 0,
    index: 0,
    currentSwiper: 0,
    currentindex: 0,
    idcurrent: '',
    count: "",
    cateToken: {},
    bannerData: {
      level: "1",
    },
    gonglv: {
      id: "",
      attack_id: "",
      p: ""
    },
    getCateIIData: {
      p: 1,
      searchKey: "",
      shop_type_one: 1,
      shop_type_two: "",
    },
    notimg: false,
    currentPage: 1,
    scrollHeight: 0,
    isdata: true,
    imgsList: []
  },
  bindGetUserInfo(res) {
    if (res.detail.userInfo) {
      this.setData({
        sision: true
      });
      this.showMsg()
    } else {
    }
  },
  onShow: function (options) {
    var that = this;
    //获取用户头像
    wx.getUserInfo({
      success: function (res) {
        let userInfo = res.userInfo
        that.setData({
          headImg: userInfo.avatarUrl
        })

      }
    })
    wx.getStorage({
      key: 'sission',
      success: function (res) {
        that.setData({
          sision: res.data
        });
        that.get_Banner();
        that.get_Cate();
        that.getList(false);
      },
      fail: function (res) {
        that.setData({
          sision: false
        })
      }
    });
    // 获取屏幕高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight * 2
        });
      }
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '正在刷新数据...',
    });
    this.getList(false)
  },
  search(e) {
    let searchKey = "getCateIIData.searchKey";
    this.setData({
      [searchKey]: e.detail.value,
      dianjiValue: e.detail.value
    })
    this.getList(false)
  },
  searchBtn(e) {
    let searchKey = "getCateIIData.searchKey";
    this.setData({
      [searchKey]: this.data.dianjiValue
    })
    that.get_Banner();
    that.get_Cate();
    that.getList(false);
  },
  showMsg() {
    var that = this
    wx.login({
      success: function (res) {
        let code_temp = res.code
        let code = "loginData.code"
        wx.getUserInfo({
          success: function (res) {
            let nickname = "loginData.nickname"
            let head_img = "loginData.head_img"
            let sex = "loginData.sex"
            let userInfo = res.userInfo
            that.setData({
              [code]: code_temp,
              [nickname]: res.userInfo.nickName,
              [sex]: res.userInfo.gender,
              [head_img]: res.userInfo.avatarUrl,
              headImg: res.userInfo.avatarUrl
            })
            that.Login();
          }
        })
      },
      fail: function (res) {
      },
      complete: function (res) {
      },
    })
  },
  Login() {
    let that = this;
    login(that.data.loginData).then(res => {
      let token = res.datas.token
      wx.setStorage({
        key: "is_member",
        data: res.datas.is_member,
      })
      wx.setStorage({
        key: 'is_shop',
        data: res.datas.is_shop,
      });
      if ([2,3].includes(res.datas.is_shop)) {
        wx.setStorage({
          key: 'is_check',
          data: true,
        });
      }
      wx.setStorage({
        key: "memberPrice",
        data: res.datas.memberPrice,
      })
      wx.setStorage({
        key: "token",
        data: token,
        success() {
          let bannerDataToken = 'bannerData.token';
          let getCateIIDataToken = 'getCateIIData.token';
          let gonglvToken = 'gonglv.token';
          let cateToken_ = 'cateToken.token';
          that.setData({
            [bannerDataToken]: token,
            getToken: token,
            [gonglvToken]: token,
            [getCateIIDataToken]: token,
            [cateToken_]: token
          });
          that.get_Banner();
          that.get_Cate();
          that.getList(false);
        }
      })
      wx.setStorage({
        key: 'sission',
        data: true,
      })
    })
  },
  bindDownLoad(e) {
    var that = this;
    if (that.data.isdata) {
      this.data.getCateIIData.p += 1;
      this.getList()
    } else {
      wx.showToast({
        title: '到底了',
        icon: 'none'
      })
    }
  },
  get_Banner() {
    //banner图
    banner(this.data.bannerData).then(res => {
      this.setData({
        imgUrls: res.datas
      })
    });
  },
  get_Cate() {
    //联动一级
    getCate(this.data.cateToken).then(res => {
      let alldata = []
      let data = alldata.concat(res.datas)
      this.setData({
        getCateList: data
      })
    })
  },
  /**
   * 获取活动或商品列表
   * @param save 是否保留已经存在的数据， 默认保留
   * @param abort 是否中断上一次未返回结果的请求， 默认不中断
   * */
  getList(save = true, abort = false) {
    wx.stopPullDownRefresh();
    if(!save) {
      this.data.getCateIIData.p = 1;
    }
    getCateII({...this.data.getCateIIData, abort}).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          arrayList: save ? this.data.arrayList.concat(res.datas.list) : res.datas.list,
          isdata: !!res.datas.more,
        });
      } else {
        this.setData({
          arrayList: []
        })
      }
    })
  },
  activityBtn(e) {
    let getCateIIDataP = "getCateIIData.p";
    let id = e.currentTarget.dataset.indexx;
    let ids = e.currentTarget.dataset.id;
    this.setData({
      aa: id
    })
    let childdata = this.data.arrayList;
    if (childdata) {
      this.setData({
        arrayList: childdata,
        [getCateIIDataP]: 1
      })
    } else {
      this.setData({
        arrayList: "",
        [getCateIIDataP]: 1
      })
    }
  },
  seachCate(e) {
    let id = e.currentTarget.dataset.indexs; //索引ID
    let ids = e.currentTarget.dataset.ids; //主键ID
    let getCateIIDataP = "getCateIIData.p";
    this.setData({
      currentIndex: id
    })
    let shop_type_two_ = "getCateIIData.shop_type_two";
    this.setData({
      [shop_type_two_]: ""
    })
    let shop_type_one_ = "getCateIIData.shop_type_one";
    this.setData({
      [shop_type_one_]: ids
    })
    let childdata = this.data.getCateList[id].children;
    if (childdata) {
      this.setData({
        childrenList: childdata,
        [getCateIIDataP]: 1
      })
      let shop_type_two_ = "getCateIIData.shop_type_two";
      this.setData({
        [shop_type_two_]: childdata[0].id
      })
    } else {
      this.setData({
        childrenList: [],
        [getCateIIDataP]: 1
      })
    }
  },
  autoSlide(e) {
    let currentIndex = e.detail.current;
    let id = this.data.getCateList[currentIndex].id;
    this.setData({
      arrayList: [],
      currentIndex: currentIndex,
      getCateIIData: {
        ...this.data.getCateIIData,
        p: 1,
        shop_type_one: id,
      },
    });
    this.getList(false, true);
  },
  partautoSlide(e) {
    this.setData({
      arrayList: []
    })
    let current = e.detail.current;
    let id = this.data.childrenList[current].id;
    let getCateIIDataP = "getCateIIData.p"
    this.setData({
      aa: current
    })
    let shop_type_two_ = "getCateIIData.shop_type_two";
    this.setData({
      [shop_type_two_]: id,
      [getCateIIDataP]: 1
    })
    this.getList(false);
  },
  businessDetails(e) {
    let shop_id = JSON.stringify(e.currentTarget.dataset.id);
    let headImg = JSON.stringify(e.currentTarget.dataset.tim.head_img);
    let shop_name = JSON.stringify(e.currentTarget.dataset.tim.shop_name);
    wx.navigateTo({
      url: './businessDetails/businessDetails?businessDetails=' + shop_id + "&headImg=" + headImg + "&shop_name=" + shop_name,
    })
  },
  swiperChange(e) {
    this.setData({
      currentSwiper: e.detail.current
    })
  },
  /*景区详情查看*/
  skipArea(e) {
    let id = 'gonglv.id';
    let attack_id = 'gonglv.attack_id'
    this.setData({
      [id]: e.currentTarget.dataset.id.id,
      [attack_id]: e.currentTarget.dataset.id.attack_id
    })
    let gonglv = JSON.stringify(this.data.gonglv)
    wx.navigateTo({
      url: '../strategy/details/details?data=' + gonglv,
    })
  },
  // 预览图片
  previewIamge(event) {
    const nowIndex = event.currentTarget.dataset.id;
    const imgList = event.currentTarget.dataset.list
    wx.previewImage({
      current: imgList[nowIndex], //当前预览的图片
      urls: imgList, //所有要预览的图片
    })
  },
  //同意
  loginShow() {
    var that = this;
    var animation = wx.createAnimation({
      duration: 800,//动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
      timingFunction: 'ease',//动画的效果 默认值是linear
    })
    this.animation = animation
    that.fadeDown();//调用隐藏动画   
    setTimeout(function () {
      that.setData({
        hideModal: true
      })
    }, 720)//先执行下滑动画，再隐藏模块
    wx.showTabBar({
      animation: false, //是否需要过渡动画
      success: res => {
      }
    })
  },
  //动画集
  fadeIn: function () {
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export()//动画实例的export方法导出动画数据传递给组件的animation属性
    })
  },
  fadeDown: function () {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //this.showMsg()
    this.bindDownLoad();
  },

  aa(e) {
  },
  bindVideoScreenChange: function (e) {
    // var that = this
    // var status = e.detail.fullScreen;
    // var play = {
    //   playVideo: false
    // }
    // if (status) {
    //   play.playVideo = true;
    // } else {
    //   that.videoContext.pause();
    // }
    // that.setData(play);
  }
})